package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControllNIC;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class NicSpezPanel extends JPanel implements ActionListener, ChangeListener {

	private static final long serialVersionUID = -8286847478674374831L;
	private JCheckBox nicBez;
	private JCheckBox ipV4;
	private JCheckBox ipV6;
	private JCheckBox mac;
	private JCheckBox subnet;
	private JCheckBox gateway;
	private JCheckBox dhcp;
	private VisuControllNIC visuControllNic;
	private int amountOfFlags;

	public NicSpezPanel(VisuControllNIC visuControllNic) {
		this.visuControllNic = visuControllNic;
		this.amountOfFlags = 0;
		init();
	}

	public void init() {
		this.nicBez = new JCheckBox("NIC-Bezeichnung", this.visuControllNic.isNicBez());
		this.nicBez.addActionListener(this);
		this.nicBez.addChangeListener(this);
		this.ipV4 = new JCheckBox("IPv4", this.visuControllNic.isIpV4());
		this.ipV4.addActionListener(this);
		this.ipV4.addChangeListener(this);
		this.ipV6 = new JCheckBox("IPv6", this.visuControllNic.isIpV6());
		this.ipV6.addActionListener(this);
		this.ipV6.addChangeListener(this);
		this.mac = new JCheckBox("MAC-Adresse", this.visuControllNic.isMac());
		this.mac.addActionListener(this);
		this.mac.addChangeListener(this);
		this.subnet = new JCheckBox("Subnet-Maske", this.visuControllNic.isSubnet());
		this.subnet.addActionListener(this);
		this.subnet.addChangeListener(this);
		this.gateway = new JCheckBox("Gateway", this.visuControllNic.isGateway());
		this.gateway.addActionListener(this);
		this.gateway.addChangeListener(this);
		this.dhcp = new JCheckBox("Hat DHCP", this.visuControllNic.isDhcp());
		this.dhcp.addActionListener(this);
		this.dhcp.addChangeListener(this);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double checkBox = 0.0;
		double lable = 1.0;
		gridBagLayout.columnWeights = new double[] { checkBox, checkBox, checkBox, lable };
		int widthCheckbox = SettingsPanel.WIDTH_CHECKBOX;
		gridBagLayout.columnWidths = new int[] { widthCheckbox, widthCheckbox, widthCheckbox, 0 };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.nicBez);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.ipV4);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.ipV6);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		y++;
		x = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.mac);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.subnet);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.gateway);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		y++;
		x = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.dhcp);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.nicBez.setSelected(flag);
		this.ipV4.setSelected(flag);
		this.ipV6.setSelected(flag);
		this.mac.setSelected(flag);
		this.subnet.setSelected(flag);
		this.dhcp.setSelected(flag);
		this.gateway.setSelected(flag);
	}

	public void actionPerformed(ActionEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.nicBez) {
			this.visuControllNic.setNicBez(this.nicBez.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.ipV4) {
			this.visuControllNic.setIpV4(this.ipV4.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.ipV6) {
			this.visuControllNic.setIpV6(this.ipV6.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.mac) {
			this.visuControllNic.setMac(this.mac.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.subnet) {
			this.visuControllNic.setSubnet(this.subnet.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.dhcp) {
			this.visuControllNic.setDhcp(this.dhcp.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.gateway) {
			this.visuControllNic.setGateway(this.gateway.isSelected());
			checkCanCrement();
		}
	}

	public void stateChanged(ChangeEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();

		if (e.getSource() == this.nicBez) {
			this.visuControllNic.setNicBez(this.nicBez.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.ipV4) {
			this.visuControllNic.setIpV4(this.ipV4.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.ipV6) {
			this.visuControllNic.setIpV6(this.ipV6.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.mac) {
			this.visuControllNic.setMac(this.mac.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.subnet) {
			this.visuControllNic.setSubnet(this.subnet.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.dhcp) {
			this.visuControllNic.setDhcp(this.dhcp.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.gateway) {
			this.visuControllNic.setGateway(this.gateway.isSelected());
			checkCanCrement();
		}
	}

	public void checkCanCrement() {
		if (canCrement()) {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(true);
		} else {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(false);
		}
	}

	public boolean canCrement() {
		return this.amountOfFlags < 8;
	}

	public boolean getNicBezState() {
		return this.nicBez.isSelected();
	}

	public boolean getIpV4State() {
		return this.ipV4.isSelected();
	}

	public boolean getIpV6State() {
		return this.ipV6.isSelected();
	}

	public boolean getMacState() {
		return this.mac.isSelected();
	}

	public boolean getSubnetState() {
		return this.subnet.isSelected();
	}

	public boolean getGatewayState() {
		return this.gateway.isSelected();
	}

	public boolean getDhcpState() {
		return this.dhcp.isSelected();
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		if (!this.nicBez.isSelected()) {
			this.nicBez.setEnabled(beEnabledFlag);
		}
		if (!this.ipV4.isSelected()) {
			this.ipV4.setEnabled(beEnabledFlag);
		}
		if (!this.ipV6.isSelected()) {
			this.ipV6.setEnabled(beEnabledFlag);
		}
		if (!this.mac.isSelected()) {
			this.mac.setEnabled(beEnabledFlag);
		}
		if (!this.subnet.isSelected()) {
			this.subnet.setEnabled(beEnabledFlag);
		}
		if (!this.gateway.isSelected()) {
			this.gateway.setEnabled(beEnabledFlag);
		}
		if (!this.dhcp.isSelected()) {
			this.dhcp.setEnabled(beEnabledFlag);
		}
	}
}
